<%inherit file="base.html"/>

<%def name="headIncludes()">
<link rel="stylesheet" href="${http_root}css/bootstrap-select.min.css">
<link rel="stylesheet" href="${http_root}css/dataTables.bootstrap.min.css">
<link rel="stylesheet" href="${http_root}css/dataTables.colVis.css">
<link rel="stylesheet" href="${http_root}css/tautulli-dataTables.css">
</%def>

<%def name="body()">
<div class='container-fluid'>
    % if config['database_is_importing']:
    <div style="text-align: center; margin-top: 20px;">
        <i class="fa fa-refresh fa-spin"></i>&nbsp; Tautulli is importing history from another database. This could take a few minutes depending on the size of your database.
        <br />
        You may leave this page and check back later.
    </div>
    % endif
    <div class='table-card-header'>
        <div class="header-bar">
            <span><i class="fa fa-history"></i> History</span>
        </div>
        <div class="button-bar">
            % if _session['user_group'] == 'admin':
            <div class="alert alert-danger alert-edit" role="alert" id="row-edit-mode-alert"><i class="fa fa-exclamation-triangle"></i>&nbsp</div>
            <div class="btn-group">
                <button class="btn btn-danger btn-edit" data-toggle="button" aria-pressed="false" autocomplete="off" id="row-edit-mode">
                    <i class="fa fa-trash-o"></i> Delete mode
                </button>
            </div>
            % endif
            % if _session['user_group'] == 'admin':
            <div class="btn-group" id="user-selection">
                <label>
                    <select name="history-user" id="history-user" multiple>
                    </select>
                </label>
            </div>
            % endif
            <div class="btn-group" data-toggle="buttons" id="media_type-selection">
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="media_type-filter" id="history-media_type-movie" value="movie" autocomplete="off"><i class="fa fa-film"></i> Movies
                </label>
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="media_type-filter" id="history-media_type-episode" value="episode" autocomplete="off"><i class="fa fa-television"></i> TV Shows
                </label>
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="media_type-filter" id="history-media_type-track" value="track" autocomplete="off"><i class="fa fa-music"></i> Music
                </label>
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="media_type-filter" id="history-media_type-live" value="live" autocomplete="off"><i class="fa fa-broadcast-tower"></i> Live TV
                </label>
            </div>
            <div class="btn-group" data-toggle="buttons" id="transcode_decision-selection">
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-direct_play" value="direct play" autocomplete="off"><i class="fa fa-play-circle"></i> Direct Play
                </label>
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-copy" value="copy" autocomplete="off"><i class="fa fa-stream"></i> Direct Stream
                </label>
                <label class="btn btn-dark btn-filter">
                    <input type="checkbox" name="transcode_decision-filter" id="history-transcode_decision-transcode" value="transcode" autocomplete="off"><i class="fa fa-server"></i> Transcode
                </label>
            </div>
            <div class="btn-group">
                <button class="btn btn-dark refresh-history-button" id="refresh-history-list"><i class="fa fa-refresh"></i> Refresh history</button>
            </div>
            <div class="btn-group colvis-button-bar"></div>
        </div>
    </div>
    <div class="table-card-back">
        <table class="display history_table" id="history_table" width="100%">
            <thead>
            <tr>
                <th align="left" id="delete_row">Delete</th>
                <th align="left" id="date">Date</th>
                <th align="left" id="friendly_name">User</th>
                <th align="left" id="ip_address">IP Address</th>
                <th align="left" id="platform">Platform</th>
                <th align="left" id="product">Product</th>
                <th align="left" id="player">Player</th>
                <th align="left" id="title">Title</th>
                <th align="left" id="started">Started</th>
                <th align="left" id="paused_counter">Paused</th>
                <th align="left" id="stopped">Stopped</th>
                <th align="left" id="play_duration">Duration</th>
                <th align="left" id="percent_complete"></th>
            </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
    </div>
</div>
</%def>

<%def name="modalIncludes()">
<div class="modal fade" id="info-modal" tabindex="-1" role="dialog" aria-labelledby="info-modal">
</div>
<div class="modal fade" id="ip-info-modal" tabindex="-1" role="dialog" aria-labelledby="ip-info-modal">
</div>
<div class="modal fade" id="confirm-modal-delete" tabindex="-1" role="dialog" aria-labelledby="confirm-modal-delete">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"><i class="fa fa-remove"></i></button>
                <h4 class="modal-title" id="myModalLabel">Confirm Delete</h4>
            </div>
            <div class="modal-body" style="text-align: center;">
                <p>Are you REALLY sure you want to delete <strong><span id="deleteCount"></span></strong> history item(s)?</p>
                <p>This is permanent and cannot be undone!</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-dark" data-dismiss="modal">Cancel</button>
                <button type="button" class="btn btn-danger btn-ok" data-dismiss="modal" id="confirm-delete">Delete</button>
            </div>
        </div>
    </div>
</div>
</%def>

<%def name="javascriptIncludes()">
<script src="${http_root}js/bootstrap-select.min.js"></script>
<script src="${http_root}js/jquery.dataTables.min.js"></script>
<script src="${http_root}js/dataTables.colVis.js"></script>
<script src="${http_root}js/dataTables.bootstrap.min.js"></script>
<script src="${http_root}js/dataTables.bootstrap.pagination.js"></script>
<script src="${http_root}js/tables/history_table.js${cache_param}"></script>
<script>
    $(document).ready(function () {
        // Load user ids and names (for the selector)
        $.ajax({
            url: 'get_user_names',
            type: 'GET',
            dataType: 'json',
            success: function (data) {
                let select = $('#history-user');
                let by_id = {};
                data.sort(function (a, b) {
                    return a.friendly_name.localeCompare(b.friendly_name);
                });
                data.forEach(function (item) {
                    select.append('<option value="' + item.user_id + '">' +
                        item.friendly_name + '</option>');
                    by_id[item.user_id] = item.friendly_name;
                });
                select.selectpicker({
                    countSelectedText: function(sel, total) {
                        if (sel === 0 || sel === total) {
                            return 'All users';
                        } else if (sel > 1) {
                            return sel + ' users';
                        } else {
                            return select.val().map(function(id) {
                                return by_id[id];
                            }).join(', ');
                        }
                    },
                    style: 'btn-dark',
                    actionsBox: true,
                    selectedTextFormat: 'count',
                    noneSelectedText: 'All users'
                });
                select.selectpicker('render');
                select.selectpicker('selectAll');
            }
        });

        let history_user_last_id = undefined;

        function loadHistoryTable(media_type, transcode_decision, selected_user_id) {
            history_table_options.ajax = {
                url: 'get_history',
                type: 'POST',
                data: function (d) {
                    return {
                        json_data: JSON.stringify(d),
                        media_type: media_type,
                        transcode_decision: transcode_decision,
                        user_id: selected_user_id
                    };
                }
            };
            history_table = $('#history_table').DataTable(history_table_options);
            var colvis = new $.fn.dataTable.ColVis(history_table, {
                buttonText: '<i class="fa fa-columns"></i> Select columns',
                buttonClass: 'btn btn-dark',
                exclude: [0, 12]
            });
            $(colvis.button()).appendTo('div.colvis-button-bar');

            clearSearchButton('history_table', history_table);

            $('#media_type-selection').on('change', function () {
                $('#media_type-selection > label').removeClass('active');
                var selected_filter = $('input[name=media_type-filter]:checked', '#media_type-selection');
                $(selected_filter).closest('label').addClass('active');
                media_type = $(selected_filter).map(function () { return $(this).val(); }).get().join(',');
                setLocalStorage('history_media_type', media_type);
                history_table.draw();
            });

            $('#transcode_decision-selection').on('change', function () {
                $('#transcode_decision-selection > label').removeClass('active');
                var selected_filter = $('input[name=transcode_decision-filter]:checked', '#transcode_decision-selection');
                $(selected_filter).closest('label').addClass('active');
                transcode_decision = $(selected_filter).map(function () { return $(this).val(); }).get().join(',');
                setLocalStorage('history_transcode_decision', transcode_decision);
                history_table.draw();
            });

            $('#history-user').on('change', function () {
                let val = $(this).val();
                if (val.length === 0 || val.length === $(this).children().length) {
                    selected_user_id = null; // if all users are selected, just send an empty list
                } else {
                    selected_user_id = val.join(",");
                }
                if (selected_user_id === history_user_last_id) {
                    return;
                }
                history_user_last_id = selected_user_id;
                history_table.draw();
            });
        }

        var selected_user_id = "${_session['user_group']}" == "admin" ? null : "${_session['user_id']}";

        var media_type = getLocalStorage('history_media_type', 'all');
        $.each(media_type.split(','), function (i, item) {
            var history_media_type = $('#history-media_type-' + item);
            history_media_type.prop('checked', true);
            history_media_type.closest('label').addClass('active');
        });

        var transcode_decision = getLocalStorage('history_transcode_decision', 'all');
        $.each(transcode_decision.split(','), function (i, item) {
            var history_transcode_decision = $('#history-transcode_decision-' + item.replace(' ', '_'));
            history_transcode_decision.prop('checked', true);
            history_transcode_decision.closest('label').addClass('active');
        });

        loadHistoryTable(media_type, transcode_decision, selected_user_id);

        % if _session['user_group'] == 'admin':
        $('#row-edit-mode').on('click', function() {
            if ($(this).hasClass('active')) {
                $(this).tooltip('destroy');

                if (history_to_delete.length > 0) {
                    $('#deleteCount').text(history_to_delete.length);
                    $('#confirm-modal-delete').modal();
                    $('#confirm-modal-delete').one('click', '#confirm-delete', function () {
                        $.ajax({
                            url: 'delete_history_rows',
                            type: 'POST',
                            data: { row_ids: history_to_delete.join(',') },
                            async: true,
                            success: function (data) {
                                var msg = "History deleted";
                                showMsg(msg, false, true, 2000);
                                history_table.draw();
                            }
                        });
                    });
                }

                $('.delete-control').each(function () {
                    $(this).addClass('hidden');
                    $('#row-edit-mode-alert').fadeOut(200);
                });

            } else {
                $(this).tooltip({
                    container: '.body-container',
                    placement: 'bottom',
                    title: 'Select rows to delete. Data is deleted upon exiting delete mode.',
                    trigger: 'manual'
                }).tooltip('show');

                history_to_delete = [];
                $('.delete-control').each(function() {
                    $(this).find('button.btn-danger').toggleClass('btn-warning').toggleClass('btn-danger');
                    $(this).removeClass('hidden');
                });
            }
        });
        % endif
    });

    $("#refresh-history-list").click(function() {
        history_table.draw();
    });
</script>
</%def>
